﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Sinacor.Infra.Tools;

namespace EndPointGenerator
{
	class Program
	{
		static void Main(string[] args)
		{
			try
			{
				List<EndPoint> endpoints = new DataAccess().LoadEndPoints();

				StringBuilder sb = new StringBuilder();
				sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
                sb.AppendLine("<endpointConfiguration>");
				sb.AppendLine("		<endpoints>");

                string path;

                foreach (EndPoint endpoint in endpoints)
                {
                    path = string.Format("Settings\\Endpoints\\{0}\\{0}.{1}.config", endpoint.Modulo, endpoint.SubModulo);

                    sb.AppendLine("			<endpoint contract=\"" + endpoint.Contrato + "\" protocol=\"" + endpoint.Protocolo + "\" endpointConfigFile=\"" + path + "\" />");
                }

				sb.AppendLine("		</endpoints>");
                sb.Append("</endpointConfiguration>");

                using (TextWriter streamWriter = new StreamWriter(@"..\..\..\..\Sinacor\Settings\Endpoints\EndPoints.config"))
				{
					streamWriter.Write(sb.ToString());
					streamWriter.Close();
				}

				Console.WriteLine("ARQUIVOS (UI e Servicos) GERADOS COM SUCESSO!!!");
			}
			catch (Exception ex)
			{
				Console.WriteLine("OCORREU UM ERRO: " + ex.Message);
			}

			Console.WriteLine("-------------------------------------");
			Console.WriteLine("PRESSIONE QUALQUER TECLA PARA SAIR...");
			Console.ReadLine();
		}
	}
}